package cn.glenn.weibo0521.controller;

import cn.glenn.weibo0521.base.respone.JsonResult;
import cn.glenn.weibo0521.base.respone.StatusCode;
import cn.glenn.weibo0521.mapper.UserMapper;
import cn.glenn.weibo0521.pojo.dto.UserLoginParam;
import cn.glenn.weibo0521.pojo.dto.UserRegParam;
import cn.glenn.weibo0521.pojo.enity.User;
import cn.glenn.weibo0521.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/v1/user/")
@Validated
@Tag(name = "01-用户管理",description = "01-用户管理")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("reg")
    @Parameters({
            @Parameter(name = "username",description = "用户名",required = true),
            @Parameter(name = "password",description = "用户密码",required = true),
            @Parameter(name = "nickname",description = "昵称",required = true)
    })
    @Operation(summary = "用户注册")
    @ApiOperationSupport(order = 1)
    public JsonResult regUser(@Valid @RequestBody UserRegParam param) {
        log.debug("reg user param:{}", param);
        int checkResult = userMapper.checkUsername(param.getUsername());
        if (checkResult == 1) {return new JsonResult(StatusCode.USERNAME_EXIST);}
        User user = new User();
        BeanUtils.copyProperties(param, user);
        user.setCreateTime(new Date());
        int insertResult = userMapper.insertUser(user);
        log.debug("insert user result:{}", insertResult);
        return JsonResult.success();
    }

    @PostMapping("login")
    @Operation(summary = "登录页面")
    @ApiOperationSupport(order = 2)
    @Parameters({
            @Parameter(name = "username",description = "用户名"),
            @Parameter(name = "password",description = "密码")
    })
    public JsonResult login(@RequestBody @Valid UserLoginParam param, HttpSession session) {
        UserVO userVO  = userMapper.loginUser(param);
        session.setAttribute("user", userVO);
        return JsonResult.success(userVO);
    }

    @GetMapping("currentUser")
    @Operation(summary = "查看当前用户")
    @ApiOperationSupport(order = 3)
    public JsonResult currentUser(HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("user");
        return JsonResult.success(user);
    }

    @GetMapping("logout")
    @Operation(summary = "登出")
    @ApiOperationSupport(order = 4)
    public void logout(HttpSession session) {
        session.removeAttribute("user");
    }


}
